tg-me.com/pythonetc/714
Last Update:
The isinstance
function checks whether an object is an instance of a class or of a subclass thereof:
class A: pass
class B(A): pass
b = B()
isinstance(b, B) # True
isinstance(b, A) # True
isinstance(b, object) # True
isinstance(b, str) # False
isinstance(str, type) # True
Type-checkers understand
isinstance
checks and use them to refine the type:a: object
reveal_type(a)
# ^ Revealed type is "builtins.object"
if isinstance(a, str):
reveal_type(a)
# ^ Revealed type is "builtins.str"
One more cool thing about
isinstance
is that you can pass in it a tuple of types to check if the object is an instance of any of them:isinstance(1, (str, int)) # True
BY Python etc
Warning: Undefined variable $i in /var/www/tg-me/post.php on line 280
Share with your friend now:
tg-me.com/pythonetc/714